﻿#light

module Dwarf_FiniteStateMachine_Generator

open System
open System.IO

let Dwarf_FiniteStateMachine_Generator (fs : FileStream, args : String[]) = 

    // constants
    let count_of_symbols_in_kilobyte = 1024

    // subsidiary objects
    let writer = new StreamWriter(fs)
    let rand = new Random()

    // current data values
    let count_of_kilobytes = (int) args.[args.Length - 1]
    let expected_file_size = (int64) count_of_kilobytes * (int64) count_of_symbols_in_kilobyte
    let mutable pattern_size = (int64) 0                                // default pattern size
    let mutable dictionary_path = ""

    for i = 0 to args.Length - 2 do
        if (args.[i] = "-patternsize") then                             // search for user-defined pattern size in args
            pattern_size <- (int64) args.[i + 1]
        if (args.[i] = "-dictionarypath") then                          // search for user-defined dictionary path in args
            dictionary_path <- args.[i + 1]

    if (pattern_size = (int64) 0) then
        raise (new Exception("The value of '-patternsize' parameter is not specified")) 
    if (dictionary_path = "") then
        raise (new Exception("The value of '-dictionarypath' parameter is not specified"))
        
    DictionaryProcessor.generateFile(dictionary_path, writer, pattern_size, rand)        // write the pattern
    writer.Write((char)1)                                                                // write delimiter
    DictionaryProcessor.generateFile(dictionary_path, writer, expected_file_size, rand)  // write the text
    
    writer.Close()

    true